package features.advance.jdk.map;

import java.util.*;

/**
 * @author LIN
 * @date 2021-08-21
 */
public class Demo {
    public static void main(String[] args) {
        Map map = new HashMap<>(16);
        demokey demokey = new demokey();
        demokey demokey1 = new demokey();
        demokey demokey2 = new demokey();

        map.put(demokey,0);
        demokey.list.add("a1");

        map.put(demokey1,1);
        demokey2.list.add("a3");
        map.put(demokey2,2);

//        demokey1.list.add("a2");

        Object o = map.get(new demokey());
        System.out.println(o);
        demokey.list.clear();
        Object o1 = map.get(new demokey());
        System.out.println(o1);
    }

}

class demokey{
    List<String> list = new ArrayList<>();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        demokey demokey = (demokey) o;
        return Objects.equals(list.size(), demokey.list.size());
    }

    @Override
    public int hashCode() {
        return Objects.hash(list);
    }
}

class HashKey{

    public static void main(String[] args) {
        String a = "a";
        String b = "b";

        int capacity = 20-1;
        Map<String,Object> map = new HashMap<>(20);
        map.put(a,null);
        System.out.println(a.hashCode());
        System.out.println(a.hashCode() & capacity);
        System.out.println(a.hashCode() % capacity);

        System.out.println(b.hashCode());
        System.out.println(b.hashCode() & capacity);
        System.out.println(b.hashCode() % capacity);


    }
}
